package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.StorageLocation;
import com.ruoyi.system.service.IStorageLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WarehouseMapper;
import com.ruoyi.system.domain.Warehouse;
import com.ruoyi.system.service.IWarehouseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仓库表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-26
 */
@Service
public class WarehouseServiceImpl implements IWarehouseService 
{
    @Autowired
    private WarehouseMapper warehouseMapper;

    @Autowired
    private IStorageLocationService iStorageLocationService;

    /**
     * 查询仓库表
     * 
     * @param warehouseId 仓库表主键
     * @return 仓库表
     */
    @Override
    public Warehouse selectWarehouseByWarehouseId(String warehouseId)
    {
        return warehouseMapper.selectWarehouseByWarehouseId(warehouseId);
    }

    /**
     * 查询仓库表列表
     * 
     * @param warehouse 仓库表
     * @return 仓库表
     */
    @Override
    public List<Warehouse> selectWarehouseList(Warehouse warehouse)
    {
        return warehouseMapper.selectWarehouseList(warehouse);
    }

    /**
     * 新增仓库表
     * 
     * @param warehouse 仓库表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertWarehouse(Warehouse warehouse)
    {
        warehouse.setWarehouseId(IdUtils.simpleUUID());
        if(warehouse.getStorageLocationList().size()>0){
            for (StorageLocation storageLocation : warehouse.getStorageLocationList()) {
                iStorageLocationService.insertStorageLocation(storageLocation);
            }
        }
        return warehouseMapper.insertWarehouse(warehouse);
    }

    /**
     * 修改仓库表
     * 
     * @param warehouse 仓库表
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateWarehouse(Warehouse warehouse)
    {
        iStorageLocationService.updateStorageLocationByWarehouseId(warehouse.getWarehouseId(),warehouse.getStorageLocationList());
        return warehouseMapper.updateWarehouse(warehouse);
    }

    /**
     * 批量删除仓库表
     * 
     * @param warehouseIds 需要删除的仓库表主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseByWarehouseIds(String[] warehouseIds)
    {
        return warehouseMapper.deleteWarehouseByWarehouseIds(warehouseIds);
    }

    /**
     * 删除仓库表信息
     * 
     * @param warehouseId 仓库表主键
     * @return 结果
     */
    @Override
    public int deleteWarehouseByWarehouseId(String warehouseId)
    {
        return warehouseMapper.deleteWarehouseByWarehouseId(warehouseId);
    }
}
